package com.ruoyi.price.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.price.domain.TbFormulaWidget;
import com.ruoyi.price.domain.TbFormulaWidgetAttr;
import com.ruoyi.price.domain.TbProject;
import com.ruoyi.price.domain.dto.TbFormulaWidgetDTO;
import com.ruoyi.price.mapper.TbFormulaWidgetMapper;
import com.ruoyi.price.mapper.TbProjectMapper;
import com.ruoyi.price.service.ITbFormulaWidgetAttrService;
import com.ruoyi.price.service.ITbFormulaWidgetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公式控件信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-16
 */
@Service
public class TbFormulaWidgetServiceImpl extends ServiceImpl<TbFormulaWidgetMapper, TbFormulaWidget> implements ITbFormulaWidgetService
{
    @Autowired
    private ITbFormulaWidgetAttrService widgetAttrService;

    @Override
    public List<TbFormulaWidgetDTO> listByFormulaId(Long id) {
        List<TbFormulaWidgetDTO> widgetDTOList = new ArrayList<>();
        // 根据公式id查询控件信息
        List<TbFormulaWidget> widgetList = list(new QueryWrapper<TbFormulaWidget>()
                .lambda()
                .eq(TbFormulaWidget::getFormualId,id));
        widgetList.forEach(item->{
            TbFormulaWidgetDTO formulaWidgetDTO = new TbFormulaWidgetDTO();
            BeanUtils.copyProperties(item,formulaWidgetDTO);

            // 查询控件属性信息
            List<TbFormulaWidgetAttr> formulaWidgetAttrList = widgetAttrService.listByFormulaIdAndWidgetId(id,item.getId());
            String attrValues = (null != formulaWidgetAttrList && !formulaWidgetAttrList.isEmpty())
                    ? formulaWidgetAttrList.stream().map(TbFormulaWidgetAttr::getAttrValue).collect(Collectors.joining(","))
                    : "";
            formulaWidgetDTO.setAttrValues(attrValues);
            widgetDTOList.add(formulaWidgetDTO);

            formulaWidgetDTO.setFormulaWidgetAttrList(formulaWidgetAttrList);
        });
        return widgetDTOList;
    }

    @Override
    public boolean removeByFormulaId(Long formulaId) {
        return remove(new QueryWrapper<TbFormulaWidget>().lambda().eq(TbFormulaWidget::getFormualId,formulaId));
    }

    @Override
    public boolean removeByFormulaIds(List<String> formulaIdList) {
        return remove(new QueryWrapper<TbFormulaWidget>().lambda().in(TbFormulaWidget::getFormualId,formulaIdList));
    }
}
